//
//  DKPhotoObject.m
//  xiaoquan
//
//  Created by 许建勇 on 15/8/31.
//  Copyright (c) 2015年 Alex. All rights reserved.
//

#import "DKPhotoModel.h"
#import "DKImageSqlite.h"

@implementation DKPhotoModel

- (instancetype)init
{
    self=[super init];
    if(self)
    {
        self.isDownload=NO;
        self.isOrigin=NO;
        self.process=0;
        self.type=DKPhotoObjectTypePicture;
    }
    return self;
}

/**
 *  开始下载图片
 */
- (void)beginDownload
{
    self.isDownload=YES;
    self.isOrigin=NO;
    [[DKImageSqlite shareImageSqlite] loadImage:self.url
                                    placeHolder:nil
                                         target:nil
                                        process:^(long long totalByte, long long receiveByte, BOOL isFinish) {
                                            if(totalByte!=-1)
                                            {
                                                self.process=receiveByte*1.0/totalByte;
                                            }
                                            if([self.delegate respondsToSelector:@selector(photoModel:didReceiveSize:totalSize:image:isFinish:)])
                                            {
                                                [self.delegate photoModel:self
                                                           didReceiveSize:receiveByte
                                                                totalSize:totalByte
                                                                    image:nil
                                                                 isFinish:NO];
                                            }
                                        }
                                 enableDownload:YES
                                     completion:^(UIImage *imageData) {
                                         self.isDownload=NO;
                                         if(imageData!=nil)
                                         {
                                             self.isOrigin=YES;
                                         }
                                         if([self.delegate respondsToSelector:@selector(photoModel:didReceiveSize:totalSize:image:isFinish:)])
                                         {
                                             [self.delegate photoModel:self
                                                        didReceiveSize:0
                                                             totalSize:0
                                                                 image:imageData
                                                              isFinish:YES];
                                         }
                                     }];
}

@end
